<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="shortcut icon" href="javaToJson.jpg"/>
  <title>Java To Json</title>
</head>

<body>
  <div id="app">
    <label for="java-input">输入Java实体类：</label>
    <textarea id="java-input" v-model="javaCode"></textarea>
    <button @click="convertToJson">转换为JSON</button>
    <textarea id="json-output" v-model="jsonOutput"></textarea>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    new Vue({
      el: '#app',
      data() {
        return {
          javaCode: '',
          jsonOutput: ''
        };
      },
      methods: {
        convertToJson() {
          try {
            const jsonObj = this.parseJavaCode(this.javaCode);
            this.jsonOutput = JSON.stringify(jsonObj, null, 2);
          } catch (error) {
            this.jsonOutput = '输入的Java实体类代码无效';
          }
        },
        parseJavaCode(javaCode) {
          const jsonObj = {};

          const lines = javaCode.trim().split('\n');
          let className = '';
          let isFieldBlock = false;

          for (let i = 0; i < lines.length; i++) {
            const line = lines[i].trim();

            if (line.startsWith('public class')) {
              const startIndex = line.indexOf('class') + 5;
              const endIndex = line.indexOf('{');
              className = line.substring(startIndex, endIndex).trim();
            } else if (line.startsWith('private') && line.includes(';')) {
              const parts = line.trim().split(' ');
              const type = parts[1];
              const fieldName = parts[2].replace(';', '');
              jsonObj[fieldName] = this.getDefaultValue(type);
              isFieldBlock = true;
            } else if (line.endsWith(';')) {
              isFieldBlock = false;
            }

          }

          return jsonObj;
        },
        getDefaultValue(type) {
          if (type === 'boolean') {
            return false;
          } else if (type === 'int' || type === 'Integer') {
            return 0;
          } else if (type === 'double' || type === 'Double' || type === 'float' || type === 'Float') {
            return 0.0;
          } else if (type === 'List<Object>') {
            return [];
          } else {
            return null;
          }
        },
        parseFieldValue(fieldValue) {
          if (fieldValue.startsWith('"') && fieldValue.endsWith('"')) {
            return fieldValue.slice(1, -1);
          } else if (fieldValue.startsWith("'") && fieldValue.endsWith("'")) {
            return fieldValue.slice(1, -1);
          } else if (fieldValue === 'true') {
            return true;
          } else if (fieldValue === 'false') {
            return false;
          } else if (!isNaN(fieldValue)) {
            return Number(fieldValue);
          } else {
            return fieldValue;
          }
        }
      }
    });
  </script>

  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 0;
    }

    #app {
      max-width: 800px;
      margin: 50px auto;
      background-color: #fff;
      padding: 30px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    label {
      font-weight: bold;
      display: block;
      margin-bottom: 10px;
    }

    textarea {
      width: 100%;
      height: 200px;
      padding: 10px;
      border: 1px solid #ccc;
      resize: vertical;
      margin-bottom: 20px;
    }

    button {
      background-color: #4caf50;
      color: #fff;
      border: none;
      padding: 10px 20px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      cursor: pointer;
    }

    #json-output {
      width: 100%;
      height: 400px;
      padding: 10px;
      border: 1px solid #ccc;
      resize: vertical;
    }
  </style>
</body>

</html>
